package com.gill.touka.feign.client;

import com.gill.touka.domain.dto.UserDTO;
import com.gill.touka.feign.fallback.FeignFallbackFactory;
import com.gill.touka.feign.inteceptor.TokenRelayFeignInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 使用feign代替restTemplate调用远程服务，虽然没有使用整合了ribbon的restTemplate，
 * 但是feign默认整合了ribbon，所以ribbon的负载均衡策略依旧生效
 * 属性1：name：当feign client请求的服务名为user-center，会根据调用的方法名，构造出url
 * 属性2：fallback/fallbackFactory：为feign调用失败配置fallback方法,和fallback相比，能捕获异常信息，两者二选一
 * 属性3：configuration：指定配置类（也可以在yml文件指定配置类）
 */
@FeignClient(name = "user-center"
        ,fallbackFactory = FeignFallbackFactory.class
        ,configuration = {TokenRelayFeignInterceptor.class})
public interface UserCenterFeignClient {

    /**
     * feign会自动构建url：http://user-center/users/{id}
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable("id") Integer id);

    /**
     * =======================================
     * 以下的接口是测试“feign如何构造多参数的请求url”
     * =======================================
     */

    /**
     * feign构造get请求：http://user-center/test/feign/get?id=xx&wxId=yy
     */
    @GetMapping("/test/feign/get")
    UserDTO testGet(@SpringQueryMap UserDTO userDTO);//spring mvc则不需要通过该注解来映射url的参数，该注解为feign设计

    /**
     * feign构造post请求：http://user-center/test/feign/post
     * {
     *  "id":1
     *  "wxId":2
     * }
     */
    @PostMapping("/test/feign/post")
    UserDTO testPost(@RequestBody UserDTO userDTO);

}
